package com.jianyun.jyzs.utils;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;

import com.jianyun.jyzs.activity.CanvasActivity;
import com.jianyun.jyzs.activity.CreateEngineerActivity;
import com.jianyun.jyzs.activity.VideoImageGridActivity;
import com.jianyun.jyzs.bean.EngineerAttach;
import com.jianyun.jyzs.bean.User;
import com.jianyun.jyzs.constant.SysConstant;

import java.io.File;
import java.util.ArrayList;



/**
 * Created by yangshijun on 2018/5/31.
 */

public class CreateEngineerUtils {

    private int cameraPermissionReqCode = 201;
    private int galleryPermissionReqCode = 202;
    private int filePermissionReqCode = 203;
    private int videoPermissionReqCode = 204;
    public static final int REQUEST_CODE_CAMERA = 11;
    public static final int REQUEST_CODE_GALLERY = 12;
    public static final int REQUEST_CODE_SELECT_VIDEO = 14;
    private CreateEngineerActivity context;

    public CreateEngineerUtils(CreateEngineerActivity activity) {
        this.context = activity;
    }

    @TargetApi(23)
    public void openVideoWithPermission() {
        if (!CommonUtils.isExitsSdcard()) {
            Toast.makeText(context, "视频需要sdcard支持！", Toast.LENGTH_SHORT).show();
            return;
        }
        try {
            if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                //有权限
                Intent intent = new Intent(context, VideoImageGridActivity.class);
                context.startActivityForResult(intent, REQUEST_CODE_SELECT_VIDEO);
            } else {
                ActivityCompat.requestPermissions(context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, videoPermissionReqCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //打开照相机
    @TargetApi(23)
    public void openCameraWithPermission() {
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA)
                == PackageManager.PERMISSION_GRANTED) {
            //有权限
            if (!CommonUtils.isExitsSdcard()) {
                Toast.makeText(context.getApplicationContext(), "拍照需要sdcard支持！", Toast.LENGTH_SHORT).show();
                return;
            }
            try {


                context.cameraFile = new File(PathUtil.imagePathName, System.currentTimeMillis() + ".jpg");
                context.cameraFile.getParentFile().mkdirs();
                context.startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE).putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(context.cameraFile)), REQUEST_CODE_CAMERA);
            } catch (Exception e) {
                    e.printStackTrace();
            }
        } else {
            ActivityCompat.requestPermissions(context,
                    new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE},
                    cameraPermissionReqCode);
        }
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        RceLog.d("lml", "requestCode=" + requestCode + "resultCode=" + resultCode);
        if (requestCode == 90 & data != null) {
            ArrayList<Parcelable> arrayList = data.getParcelableArrayListExtra("android.intent.extra.RETURN_RESULT");
            RceLog.d("lml", arrayList.toString());
            ArrayList<EngineerAttach> list = new ArrayList<>();
            for (Parcelable pl : arrayList) {
                String str = pl.toString();
                String path2 = str.substring(8, str.length());
                RceLog.d("lml", "path2" + path2);
                String extName = FileUtil.getFileExtName(pl.toString());
                EngineerAttach attach = new EngineerAttach(1, pl.toString(), path2, extName);
                list.add(attach);
            }
            RceLog.d("lml", list.toString());
            context.attachExpandView.setAttach(list);
        } else if (requestCode == 100 & data != null) {
            String path = data.getStringExtra("path");
            RceLog.d("lml", path.toString());
            ArrayList<EngineerAttach> list = new ArrayList<>();
            String extName = FileUtil.getFileExtName(path.toString());
            EngineerAttach attach = new EngineerAttach(2, "file://" + path, path, extName);
            list.add(attach);
            context.attachExpandView.setAttach(list);
        } else if (requestCode == REQUEST_CODE_CAMERA) {
            RceLog.d("lml", "REQUEST_CODE_CAMERA==95");
            if (context.cameraFile != null && context.cameraFile.length() > 0) {
                Intent intent = new Intent(context, CanvasActivity.class);
                intent.putExtra("imgPath", ImageUtils.getScaledImage(context.cameraFile.getAbsolutePath()));
                context.startActivityForResult(intent, 150);
            }
        } else if (requestCode == 150 & data != null) {
            final String imgPath = data.getStringExtra("filePath");
            RceLog.d("lml", "____" + imgPath);
            ArrayList<EngineerAttach> list = new ArrayList<>();
            String extName = FileUtil.getFileExtName(imgPath.toString());
            EngineerAttach attach = new EngineerAttach(1, "file://" + imgPath, imgPath, extName);
            list.add(attach);
            context.attachExpandView.setAttach(list);
        } else if (requestCode == REQUEST_CODE_SELECT_VIDEO & data != null) {
            String filePath = data.getStringExtra("path");
            RceLog.d("lml", "video" + filePath);
            String extName = FileUtil.getFileExtName(filePath.toString());
            EngineerAttach attach = new EngineerAttach(3, "file://" + filePath, filePath, extName);
            ArrayList<EngineerAttach> list = new ArrayList<>();
            list.add(attach);
            context.attachExpandView.setAttach(list);
        } else if (requestCode == 110 & data != null) {
            String place = data.getStringExtra(SysConstant.msg_key_string1);
            String id = data.getStringExtra(SysConstant.msg_key_string2);
            context.tvProject.setText(place);
            context.projectId = id;
            context.tvPlace.setText(null);
            context.tvPlace.setText(null);
        } else if (requestCode == 130 & data != null) {
            String type = data.getStringExtra(SysConstant.msg_key_string1);
            context.tvType.setText(type);
        } else if (requestCode == 120 & data != null) {
            String loc = data.getStringExtra(SysConstant.msg_key_string1);
            context.tvPlace.setText(loc);

        } else if (requestCode == 140 & data != null) {
            RceLog.d("lml", "140");
            ArrayList<User> list = (ArrayList<User>) data.getSerializableExtra(SysConstant.msg_key_list1);
            //ArrayList<User> list = data.getParcelableArrayListExtra(SysConstant.msg_key_list1);
            if (list == null || list.size() == 0) {
                context.tvLook.setText("全部可见");
                context.staffInfoArrayList.clear();
            } else {
                context.staffInfoArrayList.addAll(list);
                context.tvLook.setText("部分可见");
            }
        }
    }
}
